package com.chenfan.filestore.controller;

import com.chenfan.ccp.common.exception.ApiException;
import com.chenfan.filestore.dto.DownloadFileDTO;
import com.chenfan.filestore.model.pdf.WaterMaskInfo;
import com.chenfan.filestore.service.FileService;
import com.chenfan.filestore.service.PdfService;
import com.chenfan.filestore.vo.FileResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.chenfan.filestore.exception.FileStoreCode.*;

/**
 * @author xutong
 */
@Controller
@RequestMapping("file/")
public class FileWithoutReturnController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileWithoutReturnController.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private PdfService pdfService;

    @ApiOperation(value = "批量下载压缩文件", notes = "批量下载压缩文件")
    @PostMapping(value = "downloadZip")
    public void downloadZip(@RequestBody DownloadFileDTO downloadFile,
                            HttpServletRequest request, HttpServletResponse response) {
        // 如果都没有就传
        if (CollectionUtils.isEmpty(downloadFile.getFileIds()) && CollectionUtils.isEmpty(downloadFile.getFiles())) {
            return;
        }
        try {
            List<String> fileIds = downloadFile.getFileIds();
            if (!CollectionUtils.isEmpty(fileIds)) {
                String[] fileIdsArray = downloadFile.getFileIds().toArray(new String[fileIds.size()]);
                fileService.downloadToZip(downloadFile.getZipName(), fileIdsArray, request, response);
            } else {
                fileService.downloadToZip(downloadFile.getZipName(), downloadFile.getFiles(), request, response);
            }

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            LOGGER.info("退出方法>>>--downloadToZip--批量下载压缩文件");
            return;
        }
        LOGGER.info("退出方法>>>--downloadToZip--批量下载压缩文件");
    }

    @ApiOperation(value = "下载文件", notes = "下载文件")
    @GetMapping(value = "downloadWithoutReturnMsg")
    public void downloadWithoutReturnMsg(String fileId, HttpServletRequest request, HttpServletResponse response) {
        FileResult result = new FileResult();
        result.setId(fileId);
        downloadWithName(result,request,response);
    }

    @GetMapping(value = "downloadWithName")
    public void downloadWithName(@RequestBody FileResult fileResult, HttpServletRequest request, HttpServletResponse response) {
        LOGGER.info("进入方法>>>--download--下载文件 filePath=" + fileResult.getId());
        if (StringUtils.isBlank(fileResult.getId())) {
            LOGGER.info("进入方法>>>--download--下载文件----文件ID不能为空");
            return;
        }
        try {
            fileService.download(fileResult, request, response);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        LOGGER.info("退出方法>>>--download--下载文件");
    }

    /**
     * pdf水印文件
     *
     * @param filePath
     * @param response
     */
    @ApiOperation(value = "下载文件", notes = "下载文件")
    @GetMapping(value = "views/pdf/watermask")
    public void pdf(String filePath, String words, HttpServletResponse response) {
//        fileId = "1638b45f0ffe47a8a8906c68295cddd0.pdf";
//        words = "宸帆电子商务有限公司";

        WaterMaskInfo info = new WaterMaskInfo();
        info.setFileId(filePath);
        info.setWords(words);
        pdfService.waterMask(info, response);
    }


    @ApiOperation(value = "mergePdf", notes = "merge many pdf to one")
    @GetMapping(value = "mergePdf")
    public void mergePdf(String mergeName,String fileIds, HttpServletResponse response) {
        LOGGER.info("进入方法>>>--mergePdf--批量下载压缩文件 ids=" + fileIds);
        if (StringUtils.isBlank(fileIds)) {
            throw new ApiException(FILE_IDS_IS_EMPTY);
        }
        if (StringUtils.isBlank(mergeName)) {
            throw new ApiException(ILLEGAL_FILE_NAME);
        }
        try {
            String[] fileIdsArray = fileIds.split(";");
            fileService.mergePdfWithName(fileIdsArray, mergeName, response);
        } catch (Exception e) {
            throw new ApiException(PDF_MERGE_FAIL);
        }
    }
}
